package com.springapp.mvc.service;


import com.springapp.mvc.dao.StudentMapper;
import com.springapp.mvc.entity.Pager;
import com.springapp.mvc.entity.Student;
import com.springapp.mvc.entity.SystemContext;
import com.springapp.mvc.entity.UserException;
//import com.sun.tools.internal.ws.wsdl.document.Service;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


//@Service(value = "StudentService")
public class StudentServiceImpl implements StudentService {

    //引入log4j,进行日志打印
//    private static final Logger LOGGER = Logger.getLogger(StudentServiceImpl.class);
//
//    @Autowired
    StudentMapper studentMapper;

    @Override
    public int addStudent(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Override
    public Student loadByStudentName(String studentName) {
        return studentMapper.loadByStudentName(studentName);
    }

    @Override
    public Student login(String studentName, String studentPassword) {
        Student student = loadByStudentName(studentName);
        if (student == null) {
            throw new UserException("该用户不存在或者密码不正确！");
        }
        if (!student.getStudentPassword().equals(studentPassword))
            throw new UserException("该用户不存在或者密码不正确！");
        return student;
    }

    @Override
    public List<Student> listAllStudnet(int pageOffset) {
        Map<String, Object> paras = new HashMap();
        int pageSize = 15;
        paras.put("pageSize", pageSize);
        paras.put("pageOffset", pageOffset);
        return studentMapper.list(paras);
    }

    @Override
    public Pager<Student> find(String name) {
        //       System.out.println("StudentServiceImpl---->>>>>name----->>>>>" + name);
        Map<String, Object> paras = new HashMap();
        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();
        paras.put("pageSize", pageSize);
        paras.put("pageOffset", pageOffset);
        if (name != null && !"".equals(name)) {
            name = "%" + name + "%";
            //           System.out.println("StudentServiceImpl---->>>>>name----->>>>>"+name);
            paras.put("name", name);
        }
        Pager<Student> pager = new Pager<Student>();
        pager.setPageSize(pageSize);
        int totalRecord = studentMapper.count(paras);
        pager.setTotalRecord(totalRecord);
        //       LOGGER.info("----->>>>" + studentMapper.list(paras));
        pager.setDatas(studentMapper.list(paras));
        pager.setTotalPage(totalRecord / pageSize + 1);
        return pager;
    }


}
